package view;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

import controller.Counter;
import controller.Field;
import controller.FieldStats;

import vossenenkonijnen.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.*;
import javax.swing.*;

/**
 * A piechart view of the simulation
 * @author Projectgroep 5
 *
 */
public class CirkelView extends JPanel implements SimulatorView {

 // A map for storing colors for participants in the simulation
    private Map<Class, Color> colors;
    // A statistics object computing and storing simulation information
    private FieldStats stats;
    private static final Color UNKNOWN_COLOR = Color.gray;
    private ChartPanel chartPanel;

    public CirkelView() {
        
        stats = new FieldStats();
        colors = new LinkedHashMap<Class, Color>();
        
        // we put the chart into a panel
        chartPanel = new ChartPanel(createChart(new DefaultPieDataset(), "Cirkeldiagram Vossen & Konijnen", new Field(1,1)));
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        // add to the piechart view panel
        this.add(chartPanel, BorderLayout.CENTER);
                
        setVisible(true);
    }
    
    
     /**
     * Creates a chart
     */

    private JFreeChart createChart(PieDataset dataset, String title, Field field) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(
            title,      // chart title
            dataset,    // data
            true,       // include legend
            true,
            false
        );

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        
        List<Comparable> keys = dataset.getKeys();
        for(int i = 0; i< keys.size(); i++){
        	for(Class colorKey : colors.keySet()){
        		if(keys.get(i).compareTo(colorKey.getSimpleName()) == 0){
        			plot.setSectionPaint(keys.get(i), getColor(colorKey));
        		}
        	}
        }
        return chart;
    }
    
    private Color getColor(Class animalClass)
    {
        Color col = colors.get(animalClass);
        if(col == null) {
            // no color defined for this class
            return UNKNOWN_COLOR;
        }
        else {
            return col;
        }
    }


 @Override
 public void setColor(Class animalClass, Color color)
    {
        colors.put(animalClass, color);
    }
 
 
 @Override
 public boolean isViable(Field field)
    {
        return stats.isViable(field);
    }
 
 
 @Override
 public void showStatus(int step, Field field)
    {
        if(!isVisible()) {
            setVisible(true);
        }
        
        stats.reset();

        for(int row = 0; row < field.getDepth(); row++) {
            for(int col = 0; col < field.getWidth(); col++) {
                Actor animal = (Actor)field.getObjectAt(row, col);
                if(animal != null) {
                    stats.incrementCount(animal.getClass());
                }
            }
        }
        stats.countFinished();
        
        DefaultPieDataset dataSet = new DefaultPieDataset();
        
        HashMap<Class, Counter> counters = stats.getActorAndCount(field);
        for(Class key : counters.keySet()) {
            Counter info = counters.get(key);
            dataSet.setValue(key.getSimpleName(), info.getCount());
        }
        
        JFreeChart chart = this.createChart(dataSet, "Cirkeldiagram Vossen & Konijnen", field);
        
        this.chartPanel.setChart(chart);
    }
}